﻿using NL;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;

namespace NL.RPG
{
    [RequireComponent(typeof(VisualEffect))]
    public class VFXSpurter : MonoBehaviour
    {

        //缓存最大数量..--一次发射得最大数量.
        const int MAX_BUFF= 500;
        // 缓存....
        //伤害数字结构..(pos,data)   pos位置. hp
        Vector4[] DATA_ALL=new Vector4[MAX_BUFF];


        int NowLength;


        public   void AddOne(Vector3 pos, int hp, HudType type = HudType.hurt)
        {
            if (NowLength >= MAX_BUFF) return;

            var index = NowLength++;

            DATA_ALL[index] = pos;
            DATA_ALL[index].w = hp;//<<8 + (byte)type;

   
        }

        [Header("播放速度")]
        public float PlayRate = 1f;

        //[Header("类型(0:伤害; 1:暴击伤害; 2:治疗;   3:闪避    ;4:秒杀;  5:免疫)")]
        //[SerializeField] Color[] ColorArr;

   
  

        ////颜色缓存.
        //GraphicsBuffer _ColorBuffer;


        int countID = Shader.PropertyToID("Count");




        VisualEffect _Owner;
        private void Awake()
        {
            _Owner=GetComponent<VisualEffect>();
        }

        GraphicsBuffer _HudBuffer_All;
        void Start()
        {

  
            _HudBuffer_All = new GraphicsBuffer(GraphicsBuffer.Target.Structured , MAX_BUFF, 16);// pos(12) 
            _Owner.SetGraphicsBuffer("BufferAll", _HudBuffer_All);



            //_ColorBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured,16, 16);
            //_Owner.SetGraphicsBuffer("ColorData", _ColorBuffer);
        }
        void OnDestroy()
        {
            //_HudBuffer?.Release();
            _HudBuffer_All?.Release();

            //_ColorBuffer?.Release();
        }
        void Update()
        {
            _Flush();
        }

        void _Flush()
        {
            
            if (NowLength == 0) return;

            //LOG.Add($"[CombatHud._Flush]:ALL.Length:{NowLength}");

            _HudBuffer_All.SetData(DATA_ALL);


            _Owner.SetInt(countID, NowLength);// Count是指定此次要生成的数字粒子数量,正常游戏过程中每帧的数字并不一样,在测试这里是一样的

            _Owner.playRate = PlayRate;
            _Owner.Play();

            NowLength = 0;

        }


    }
}